package siscom.database.PostgreSQL;

import java.sql.Connection;
import java.sql.SQLException;
import org.postgresql.ds.PGPoolingDataSource;
import siscom.database.Conexao;
import siscom.util.Configuracoes;

/**
 *
 * @author Carlos Ribeiro
 */
public class ConexaoPostgres implements Conexao {

    private static Connection conexao = null;
    private static PGPoolingDataSource source = new PGPoolingDataSource();

    static {
        source.setDataSourceName("PGSQLPOOL");
        source.setServerName(Configuracoes.getHost());
        source.setDatabaseName(Configuracoes.getDbName());
        source.setUser(Configuracoes.getUsuarioBanco());
        source.setPassword(Configuracoes.getSenhaBanco());
        source.setMaxConnections(Configuracoes.getMaxConnection());
    }

    @Override
    public Connection getConexao() throws SQLException{
        if (conexao == null) {
            conexao = source.getConnection();
            conexao.setAutoCommit(false);
        }
        return conexao;
    }

    @Override
    public String getLastInsertID(String tabela, String campo) {
        return "SELECT CURRVAL(pg_get_serial_sequence('" + tabela + "','"
                + campo + "'))";
    }
}
